import qs from 'qs'
import router from '../../router'

const tagsView = {
  state: {
    visitedViews: [],
    cachedViews: [],
    listRefresh: []  /* 待刷新列表页path数组 */
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      let changeQuery = -1;
      if (state.visitedViews.some((v, index) => {
        let vPath = v.path.split('?'), viewPath = view.path.split('?');
        /* 相同路径不同参数 */
        if (vPath[0] === viewPath[0] && vPath[1] !== viewPath[1]) {
          changeQuery = index;
        }
        return v.path === view.path;
      })) return
      let query = qs.stringify(view.query);
      let newView = {
        name: view.name,
        path: view.path + (query ? ('?' + qs.stringify(view.query)) : ''),
        title: view.name || 'no-name'
      }
      if (changeQuery != -1) {  /* 已有路径更新参数 */
        state.visitedViews[changeQuery] = newView;
      } else {  /* 新增tag */
        state.visitedViews.push(newView);
        if (!view.noCache) {
          state.cachedViews.push(view.name)
        }
      }

    },
    DEL_VISITED_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      // debugger
      for (const i of state.cachedViews) {
        if (i === view.name) {
          // debugger
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews = state.visitedViews.slice(i, i + 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews = state.cachedViews.slice(index, i + 1)
          break
        }
      }
    },
    DEL_ALL_VIEWS: state => {
      state.visitedViews = []
      state.cachedViews = []
    },
    ADD_LIST_REFRESH: (state, value) => {
      if (state.listRefresh.indexOf(value) === -1) {
        state.listRefresh.push(value)
      }
    },
    DEL_LIST_REFRESH: (state, value) => {
      state.listRefresh.splice(state.listRefresh.indexOf(value),1)
    }
  },
  actions: {
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({ commit, state }, view) {
      return new Promise(resolve => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews({ commit, state }, view) {
      return new Promise(resolve => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews({ commit, state }) {
      return new Promise(resolve => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    },
    /* 移除当前页tag */
    delActiveViews({ commit, state }) {
      let activeRoute = router.app._route;
      let view = {
        path: activeRoute.fullPath,
        naem: activeRoute.name
      }
      return new Promise(resolve => {
        commit('DEL_VISITED_VIEWS', view)
        const latestView = state.visitedViews.slice(-1)[0]
        if (latestView) {
          router.push(latestView.path)
        } else {
          router.push('/')
        }
        resolve([...state.visitedViews]);
      })
    }
  }
}

export default tagsView
